package com.dk.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.ArrayList;
import java.util.List;

public class UserRealm extends AuthorizingRealm {
    //登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        //假设数据库账号密码
        String uname="叫兽";
        String salt="b70979";
        String pwd ="79f0f40434371adf8c857a811151d543";
        //判断账号或者密码
        //1.判断用户名
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            if (!token.getUsername().equals(uname)){
                return null;
            }
            //判断密码
        return new SimpleAuthenticationInfo(uname,pwd, ByteSource.Util.bytes(salt),"");
    }

    //权限
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        //授权
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        List<String>perms = new ArrayList<>();
        perms.add("delete_perm");
        authorizationInfo.addStringPermissions(perms);
        return authorizationInfo;
    }

}
